import { downloadSheet } from "./download.js";

async function getUPVideo(UPID, limits = 1, delay = 100, bookType = "xlsx") {
    //limits:设置并发数为1，减小爬取的速度

    // 请求一次获取总页数
    let num = await fetch("https://api.bilibili.com/x/space/arc/search?mid=" + UPID + "&ps=30&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp")
        .then((res) => res.json())
        .then((res) => Math.ceil(res.data.page.count / 30));

    let res = await spider.Ajax({
        urls: [...Array(num).keys()].map((i) => "https://api.bilibili.com/x/space/arc/search?mid=" + UPID + "&ps=30&tid=0&pn=" + (i + 1) + "&keyword=&order=pubdate&jsonp=jsonp"),
        returnType: "json",
        type: "start",
        limits,
        time: delay,
        options: {
            cache: "force-cache", // 强制从缓存中取出结果
        },
    });

    // 继续上面的代码
    let afterParse = res.map((res1) => res1.data.list.vlist).flat();
    console.log(afterParse);
    //打印结果

    // 通过 JSpider 的Script函数加载 sheetjs 方便我们保存为xlsx文件
    // 下面的部分为固定不变的部分，可以复用
    await downloadSheet(afterParse, "B站UP主" + UPID + "的视频信息-" + new Date().getTime(), bookType);
}
export { getUPVideo };
